//app.js
import urls from '/utils/urls.js'
import loginFn from '/utils/login.js';
App({
	onLaunch: function() {
		console.log('app onLaunch');
		var that = this;
		loginFn();

		//获取手机信息
		wx.getSystemInfo({
			success(res) {
				that.globalData.systemInfo = res
			}
		})
		this.getall();

	},
	globalData: {
		userInfo: null, //用户信息
		location: {
			latitude: '',
			longitude: ''
		}, //用户位置
		allselect: [], //所有选项
		classtype: [], //所有课程类型
		schoolBrand: [], //连锁机构
		systemInfo: '', //手机系统信息
	},
	getall: function(loginInfo) {
		var that = this
		this.request({
			url: 'subject/list',
			sCallback: (res) => {
				if(res.code == 1) {
					that.globalData.allselect = res.data;
				}
			}
		})

		this.request({
			url: 'course/type',
			sCallback: (res) => {
				if(res.code == 1) {
					that.globalData.classtype = res.data;
				}
			}
		})

		this.request({ //机构连锁
			url: 'school/brand',
			sCallback: (res) => {
				if(res.code == 1) {
					that.globalData.schoolBrand = res.data
				}
			}
		})
	},
	//访客追踪开始
	visitorEnter(params, callback){
		// console.log('统计访客开始时间');
		this.request({
			url: 'visitor/enter',
			data: params,
			sCallback: (res) => {
				if(res.code == 1) {
					callback && callback(res.data.visitorId);
				}
			}
		})
	},
	//访客追踪结束
	visitorLeave(visitorId, callback){
		// console.log('统计访客离开时间');
		this.request({
			url: 'visitor/leave',
			data: {
				visitorId: visitorId
			},
			sCallback: (res) => {
				if(res.code == 1) {
					callback && callback(res.data);
				}
			}
		})
	},
	//获取元素绑定的数据
	getBindParam(e, name) {
		return e.currentTarget.dataset[name];
	},
	//获取扫码进来的参数
	getSceneData(scene){
		console.log('解释前scene', scene)
		let data = {};
		if(scene) {
			scene = decodeURIComponent(scene);
			console.log('解释后scene', scene)
			let params = scene.split('&');
			let paramsLen = params.length;
			if(paramsLen > 0) {
				for (var i = 0; i < paramsLen; i++) {
					if(params[i]) {
						let param = params[i].split('=');
						data[param[0]] = param[1];
					}
				}
			}
		}

		return data;
		
	},
	//弹窗提示
	alert(tips, callback){
		wx.showModal({
			title: '提示',
			content: tips,
			showCancel: false,
			success: (res) => {
				callback && callback();
			}
		})
	},
	//封装请求,默认调新接口
	request(params) {
		var that = this;
		let data = params.data ? params.data : {};


		let token = wx.getStorageSync('token');

		//默认调用新接口
		let url = urls.apiUrlV2+params.url;
		if(params.newApi === false) {
			url = params.url;
		}

		data.lat = this.globalData.location.latitude;
		data.lng = this.globalData.location.longitude;

		wx.request({
			// 必需
			url: url,
			method: 'POST',
			data: data,
			header: {
				'Content-Type': 'application/json',
				'appid': 'wx7e6758673fa45bc8',
				'token': token,
				'platform': 'weixin'
			},
			success: (res) => {
				params.sCallback && params.sCallback(res.data);
			},
			fail: (res) => {
				params.eCallback ? params.eCallback(res) : console.log('服务器繁忙', res);
			}
		})
	},
	//上传图片
	uploadImage(tempFilePaths, callback) {
		wx.uploadFile({
			url: urls.apiUrlV2+'upload/index', // 真实的接口地址
			filePath: tempFilePaths,
			header: {
				'Content-Type': 'application/json',
				'appid': appId,
				'token': token,
				'platform': platform
			},
			name: 'upfile', //注意这里是upfile
			formData: {
				user: 'test'
			},
			success(e) {
				callback && callback(JSON.parse(e.data));
			}
		})
	},
	//获取定位
	getLocation(callback) { 
        var that = this;
        if(that.globalData.location.latitude) {
            callback && callback();
        } else {
            // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.record" 这个 scope
            wx.getSetting({
                success(res) {
                    if (!res.authSetting['scope.userLocation']) {
                        wx.authorize({
                            scope: 'scope.userLocation',
                            success(res) {
                                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                                wx.getLocation({
                                    type: 'wgs84',
                                    success(res) {
                                        that.globalData.location = res
                                        //课程列表
                                        callback && callback();
                                    },
                                    fail(res) {
                                        callback && callback();
                                    }
                                })
                            },
                            fail(res) {
                                wx.showModal({
                                    title: '提示',
                                    content: '你未授权获取位置？无法更好为你提供信息，是否打开授权？',
                                    success: function(e){
                                        if(e.confirm) {
                                            wx.openSetting({
                                            	success(res) {
                                            		if(res.authSetting['scope.userLocation']) {
                                            			wx.getLocation({
						                                    type: 'wgs84',
						                                    success(res) {
						                                        that.globalData.location = res
						                                        //课程列表
						                                        callback && callback();
						                                    },
						                                    fail(res) {
						                                        callback && callback();
						                                    }
						                                })
                                            		}
                                            	}
                                            });
                                        } else {
                                        	console.log('cancel')
                                        	callback && callback();
                                        }
                                    }
                                });
                                callback && callback();
                            }
                        })
                    } else {
                        wx.getLocation({
                            type: 'wgs84',
                            success(res) {
                                that.globalData.location = res
                                //课程列表
                                callback && callback();
                            },
                            fail(res) {
                                callback && callback();
                            }
                        })
                    }
                },
                fail: function() {
                    callback && callback();
                }
            })
        }

    },

})
